/**  
* @Title: BasicAction.java
* @Package org.mtons.controller
* @Description: TODO
* @author ma jinjian  
* @date 2016年1月17日 下午6:15:02
* @version V1.0  
*/ 
package cn.net.mtons.web;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.net.mtons.model.entity.vo.AjaxResultVO;

/**
 * @ClassName: BasicAction
 * @Description: 基础Action类
 * @author ma jinjian
 * @date 2016年1月17日 下午6:15:02
 *
 */
public abstract class BasicAction {

	private static Logger logger = Logger.getLogger("command");
	
	/**
	 * 验证表单数据的合法性
	 * @Author cl
	 * @param form - 表单
	 * @param resultVO - 返回结果
	 * @return 如果验证失败返回 false，并将验证信息填充至 resultVO中,否则返回true
	 */
	public boolean validate( Object form, AjaxResultVO resultVO ) {
		logger.debug("");
		return true;
	}
	
	public boolean validate( Object form, MultipartFile[] files, AjaxResultVO resultVO ) {
		return true;
	}
	
	public boolean validate( Object form, CommonsMultipartFile file, AjaxResultVO resultVO ) {
		
		return true;
	}
	
	/**
	 * 获取操作信息
	 * @return
	 */
	public String getOperationInfo() {
		Object temp = getHttpServletRequest().getAttribute( "operationInfo" );
		return temp == null ? "" : temp.toString();
	}
	
	/**
	 * 设置操作信息
	 * @param operationInfo
	 */
	public void setOperationInfo( String operationInfo ) {
		getHttpServletRequest().setAttribute( "operationInfo", operationInfo );
	}
	
	/**
	 * 是否含有操作信息
	 * @return
	 */
	public boolean hasOperationInfo() {
		Object temp = getHttpServletRequest().getAttribute( "operationInfo" );
		return temp == null ? false : true;
	}
	
	
	/**
	 * @return
	 */
	public HttpServletRequest getHttpServletRequest() {
		return  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public HttpSession getHttpSession() {
		return getHttpServletRequest().getSession();
	}
	
	public HttpSession getHttpSession( boolean agr ) {
		return getHttpServletRequest().getSession( agr );
	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T getSessionAttribute( String key, Class<T> clazz ) {
		Object obj = getHttpSession().getAttribute( key );
		if ( obj == null ) return null;
		else return (T) obj;
	}
	
	public String getParameter( String key ) {
		return this.getHttpServletRequest().getParameter( key );
	}
	
	/**
	 * 从spring容器中获取指定的bean
	 * @param beanName
	 * @param t
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T> T getBean( String beanName, Class<T> clazz ){
		ApplicationContext ac = ContextLoader.getCurrentWebApplicationContext();
		return (T) ac.getBean( beanName );
	}
	
	
	protected <T> T createBean( String beanName, Class<T> clazz ) {
		return getBean( beanName, clazz );
	}
	
	/**
	 * 从spring容器中获取指定的bean
	 * @param beanName
	 * @param t
	 * @return
	 */
	protected Object getBean( String beanName ){
		ApplicationContext ac = ContextLoader.getCurrentWebApplicationContext();
		return ac.getBean( beanName );
	}
	
	/**
	 * springmvc中已经拥有自动封装form的功能，不建议使用该函数进行封装
	 * 功能实现使用反射机制实现,赋值时采用直接赋值方式，且数据类型仅支持String，及String[]两种
	 * @Author cl
	 * @param clazz
	 * @return
	 */
	protected <T> T createForm( Class<T> clazz ) {
		T t = null;
		try {
			t = clazz.newInstance();
			Field[] fields = clazz.getDeclaredFields();
			HttpServletRequest request = getHttpServletRequest();
			for ( Field field : fields ) {
				field.setAccessible( true );
				if ( field.getType().isArray() ) {
					String[] values = request.getParameterValues( field.getName() );
					field.set( t, values ); 
				} else {
					String value = request.getParameter( field.getName() );
					field.set( t, value ); 
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return t;
	}
}
